<?php
/**
 * Created by PhpStorm.
 * User: Abdulaziz
 * Date: 15/7/6
 * Time: 下午12:08
 */


class LinkAction extends CommonAction{
    /*导入友情链接模板*/
    public function index()
    {
        $Photo  =   M('link');
        $data   =   $Photo->order('add_time desc')->select();
        $this->assign('data', $data);
        $this->display();
    }
    /*导入友情链接编辑模板*/
    public function edit()
    {
        $con['id']=I('id');
        $pic_name=M('link')->where($con)->select();
        $this->assign('data', $pic_name);
        $this->display();
    }
    /*友情链接编辑处理*/
    public function run_edit(){

        if (!empty($_FILES['lin_img']['name'])) {
            //如果有文件上传 上传附件
            $pic_name=$_POST['lin_old_img'];
            $pic_name=$_SERVER['DOCUMENT_ROOT'].__ROOT__.'/'. APP_NAME. '/uploads/lin_images/'.$pic_name;
            unlink($pic_name);
            $this->_upload();
        }else{

            if(M(link)->save($_POST)){
                $this->success('编辑成功',U('index'));
            }else{
                $this->error('编辑失败');
            }
        }

        }
    /*导入添加友情链接模板*/
    public function addlink()
    {
        $this->display();
    }
    /*处理添加友情链接*/
    public function run_addlink() {
        if (!empty($_FILES)) {
            //如果有文件上传 上传附件
            $this->_upload();
        }
    }
    /*上传网站logo图片*/
    protected function _upload() {
        import('ORG.Util.UploadFile');
        //导入上传类
        $upload = new UploadFile();
        //设置上传文件大小
        $upload->maxSize            = 3292200;
        //设置上传文件类型
        $upload->allowExts          = explode(',', 'jpg,gif,png,jpeg');
        //设置附件上传目录
        $upload->savePath           = './gulbagh/uploads/lin_images/';
        if (!$upload->upload()) {
            //捕获上传异常
            $this->error($upload->getErrorMsg());
        } else {
            //取得成功上传的文件信息
            $uploadList = $upload->getUploadFileInfo();
            $_POST['lin_img'] =$uploadList[0]['savename'];
        }
        $model  = M('link');
        //保存当前数据对象
        $data['title']= $_POST['title'];
        $data['lin_url']= $_POST['lin_url'];
        $data['lin_img']= $_POST['lin_img'];
        $data['lin_sort']= $_POST['lin_sort'];
        $data['adder_name']= session('username');
        $data['adder_ip']= get_client_ip();
        $data['add_time']= date("Y-m-d" ,time());
        $data_id=array_merge($data,array('id'=>$_POST['id']));
        if($_POST['id']){
            $list=$model->save($data_id);
        }else{
            $list=$model->add($data);
        }
        if ($list !== false) {
            $this->success('上传成功！',U('index'));
        } else {
            $this->error('上传失败!');
        }
    }
    /*删除*/
    public function delete(){
        $con['id']=I('id');
        $pic_name=M('link')->where($con)->find();

        $pic_name=$pic_name['lin_img'];
        $pic_name=$_SERVER['DOCUMENT_ROOT'].__ROOT__.'/'. APP_NAME. '/uploads/lin_images/'.$pic_name;
        $delete=M('link')->where($con)->delete();
        if(file_exists($pic_name)){
           $del=unlink($pic_name);
            if($del && $delete){
                $this->success('删除成功');
            }else{
                $this->error('出现错误');
            }
        }else{
                $this->error('网站logo图片不存在');
        }
    }
}
?>